import axios from "axios"

export default {
    namespaced: true,
    state: {
        catagtory: [], //分类频道列表
        currentCatagtory:'' //当前激活
    },
    mutations: {
        updataCatagory(state,payload) { 
            state.catagtory = payload // 更新分类数据
        },
        updataCurrentCatagtory(state,payload) { 
            state.currentCatagtory = payload // 更新当前激活
        }
    },
    actions: {
        //定义获取频道列表的action,  将第一个频道激活
        async getCatagtory(cxt) { 
            const { data: { data: { channels} },status } = await axios.get('http://toutiao.itheima.net/v1_0/channels');
            if (status !== 200) return console.log('请求失败');
            cxt.commit('updataCatagory', channels);
            cxt.commit('updataCurrentCatagtory', channels[0].id);
            
        }
    }
}